package com.example.supertest.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    // 处理根路径，重定向到登录页面
    @GetMapping("/")
    public String redirectToLogin() {
        return "redirect:/login";
    }

    // 登录页面
    @GetMapping("/login")
    public String showLoginPage() {
        return "login";
    }

    // 处理登录请求
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model) {
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/manager", "root", "yjn2003126.");
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM user WHERE users =? AND pwd =?")) {
            statement.setString(1, username);
            statement.setString(2, password);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    model.addAttribute("username", username);
                    return "login-success";
                } else {
                    model.addAttribute("error", "Invalid username or password");
                    return "login";
                }
            }
        } catch (SQLException e) {
            model.addAttribute("error", "Database error");
            return "login";
        }
    }

    // 注册页面
    @GetMapping("/register")
    public String showRegisterPage() {
        return "register";
    }

    // 处理注册请求，添加了用户名重复检查逻辑
    @PostMapping("/register")
    public String register(@RequestParam String newUsername, @RequestParam String newPassword, Model model) {
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/manager", "root", "yjn2003126.");
             PreparedStatement checkStatement = connection.prepareStatement("SELECT * FROM user WHERE users =?");
             PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO user (users, pwd) VALUES (?,?)")) {
            // 先检查用户名是否已存在
            checkStatement.setString(1, newUsername);
            try (ResultSet resultSet = checkStatement.executeQuery()) {
                if (resultSet.next()) {
                    model.addAttribute("error", "该用户名已存在，请重新选择用户名");
                    return "register";
                } else {
                    // 若不存在，则插入新用户信息
                    insertStatement.setString(1, newUsername);
                    insertStatement.setString(2, newPassword);
                    insertStatement.executeUpdate();
                    return "register-success";
                }
            }
        } catch (SQLException e) {
            model.addAttribute("error", "注册时数据库出现错误，请稍后再试");
            return "register";
        }
    }
}